<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class BoxController extends AdminbaseController{
	
	// 微信用户列表
	public function index(){
		$Box  = M('Box');
       
        if(isset($_GET['is_fenpei'])){
        	$where['club_id'] = (int)$_GET['is_fenpei'] == 1 ? array('GT',0) : 0;
        }
        $count = $Box->where($where)->count();

        $page  = $this->page($count, 16);
		$list = $Box->where($where)->order('add_time desc')->limit($page->firstRow, $page->listRows)->select();
        
		$this->assign("page", $page->show('Admin'));
	    $this->assign('list',$list);
	    $this->assign('club_list',D('Club')->getClubList());
	    $this->display();

	}

		//添加球柜
	public function add_box(){
		$id        = I('get.id');
		//$club_name = M('Club')->where('id = '.$id)->getField('name');

		$this->assign('id',$id);
		//$this->assign('club_name',$club_name);
		$this->display();
	}

	//添加智能柜
	public function addBox(){
		$Box = M('box'); 
		//$box_info['club_id']  = I('post.id');
		$box_info['add_time'] = time();
		$box_info['number']   = I('post.number');
	    
	    $control_board_number = I('post.control_board_number');

        if($box_id = M('Box')->add($box_info)){
            //添加控制板
            $control_board_info[0]['box_id'] = $box_id;
            $control_board_info[0]['number'] = $control_board_number[0]; 
            $control_board_info[0]['order']  = 1;

            $control_board_info[1]['box_id'] = $box_id;
            $control_board_info[1]['number'] = $control_board_number[1]; 
            $control_board_info[1]['order']  = 2;
            
            if($result = M('control_board')->addAll($control_board_info)){
            	$this->addLuck(4,$box_id,$box_info['number'],$control_board_number);
			    $this->success("添加成功！", U("Box/index"));die;
            }
        	
		}
		
		$this->error("添加失败！");
	}



	//分配球柜
	public function box_fenpei(){
		$club_id = I('post.club_id');
		$id  = I('post.id');

		if(M('Box')->where('id = '.$id)->save(array('club_id' => $club_id))){
		    $this->success("分配成功！");
		}else{
		    $this->success("分配失败！");
		}
	}
    
    //添加智能锁
    private function addLuck($count,$box_id,$box_number,$number){
    	$number = explode(',', $number);
    	$Lock = M('lock');  
        $number = explode(',', $val['number']);

		for ($i = 1; $i < 5 ; $i++) { 
        	$lock_info['box_id']               = $box_id;
        	$lock_info['box_number']           = $box_number;
        	$lock_info['control_board_number'] = $number[0];
        	$lock_info['lock_number']          = $i;
        	$lock_info['number']               = $number[0].$i;
            $lock_info['status']               = 0;
            $lock_info['user_id']              = 0;
            $Lock->add($lock_info);
        }


		for ($i = 5; $i < 9 ; $i++) { 
        	$lock_info['box_id']               = $box_id;
        	$lock_info['box_number']           = $box_number;
        	$lock_info['control_board_number'] = $number[1];
        	$lock_info['lock_number']          = $i;
        	$lock_info['number']               = $number[1].$i;
            $lock_info['status']               = 0;
            $lock_info['user_id']              = 0;
            $Lock->add($lock_info);
        }
    }

}